package com.sky.controller.user;

import com.sky.dto.UserLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO loginDTO){

        UserLoginVO u = userService.login(loginDTO);
        return Result.success(u);

    }


    //登出功能
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        // 1. 从请求头中获取token
        String token = request.getHeader(jwtProperties.getUserTokenName());
        redisTemplate.delete("User_token:" + token);
        return Result.success();
    }
}
